package cn.tx.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.tx.model.Product;
import cn.tx.model.ProductType;
import cn.tx.model.Supplier;
import cn.tx.query.ProductQuery;
import cn.tx.service.ProductService;
import cn.tx.service.SupplierService;
import cn.tx.util.JSONWriter;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class ProductAction extends BaseAction implements ModelDriven<Product>{
	
	private Product product = new Product();
	
	private ProductService productService;
	
	private SupplierService supplierService;
	
	
	
	
	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * 查询对象
	 */
	private ProductQuery erpQuery = new ProductQuery();
	
	
	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	

	public ProductQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(ProductQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	/**
	 * 查询员工的列表
	 * @return
	 */
	public String product_list(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		Page page = productService.selectObjectByConditionPage(erpQuery, exclude);
		List<Supplier> slist = supplierService.listAll();
		context.put("page", page);
		context.put("suppliers", slist);
		
		return SUCCESS;
	}
	
	/**
	 * 根据供应商查询下面商品类别
	 * @return
	 */
	public void product_loadpt(){
		if(erpQuery.getSupplierId() != null){
			Supplier supplier = supplierService.getObjectById(erpQuery.getSupplierId());
			//获得指定供应商下的商品类别
			Set<ProductType> pts = supplier.getPts();
			JSONWriter.writeJSONArray(pts, response, new String[]{"supplier","pset"});
		}else{
			JSONWriter.writeJSONArray(new ArrayList(), response,new String[]{"supplier","pset"});
		}
		
		
			
		
		
		
	}
	public void product_getById(){
		product = productService.getObjectById(erpQuery.getProductId());
		JSONWriter.writeJSONObj(product, response, new String[]{"productType"});
	}
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String product_input(){
		ActionContext context = ActionContext.getContext();
		List<Supplier> slist = supplierService.listAll();
		context.put("suppliers", slist);
		return SUCCESS;
	}
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String product_preupdate(){
		//查询所有的供应商
		ActionContext context = ActionContext.getContext();
		List<Supplier> slist = supplierService.listAll();
		context.put("suppliers", slist);
		product = productService.getObjectById(product.getProductId());
		//根据当前商品的所属的供应商的商品类别
		Supplier supplier = product.getProductType().getSupplier();
		Set<ProductType> pts = supplier.getPts();
		context.put("pts", pts);
		
		return SUCCESS;
	}
	/**
	 * 保存
	 * @return
	 */
	public void product_save(){
		productService.save(product);
		JSONWriter.writeString("yes", response);
	}
	/**
	 * 修改
	 * @return
	 */
	public void product_update(){
		productService.update(product);
		JSONWriter.writeString("yes", response);
	}
	/**
	 * 修改
	 * @return
	 */
	public void product_delete(){
		product = productService.getObjectById(product.getProductId());
		productService.delete(product);
		
	}

	@Override
	public Product getModel() {
		return product;
	}
	
	
	
	

}
